package com.hzcx.app.simplechat.ui.publicui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.event.ImageEvent;
import com.hzcx.app.simplechat.event.VideoEvent;
import com.hzcx.app.simplechat.util.AnimUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import butterknife.BindView;

/**
 * 分享弹窗
 */
public class CameraActivity extends BaseActivity {

    @BindView(R.id.jcameraview)
    JCameraView jCameraView;
    public static final String IMAGE_PATH = Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera";


    @Override
    public int getLayout() {
        return R.layout.activity_camera;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void initView() {

//        if (Build.VERSION.SDK_INT >= 19) {
//            View decorView = getWindow().getDecorView();
//            decorView.setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//        } else {
//            View decorView = getWindow().getDecorView();
//            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
//            decorView.setSystemUiVisibility(option);
//        }
        jCameraView.postDelayed(new Runnable() {
            @Override
            public void run() {
                jCameraView.setVisibility(View.VISIBLE);
            }
        },700);

        //1.1.1
        //设置视频保存路径
        jCameraView.setSaveVideoPath(IMAGE_PATH);

        //设置只能录像或只能拍照或两种都可以（默认两种都可以）
        jCameraView.setFeatures(JCameraView.BUTTON_STATE_BOTH);

        //设置视频质量
        jCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
        //JCameraView监听
        jCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                Log.i("CJT", "open camera error");
            }

            @Override
            public void AudioPermissionError() {
                //没有录取权限回调
                Log.i("CJT", "AudioPermissionError");
            }
        });
        jCameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                String path = saveBitmap(bitmap, IMAGE_PATH,CameraActivity.this);
                ImageEvent imageEvent = new ImageEvent();
                imageEvent.path = path;
                EventBus.getDefault().post(imageEvent);
                finish();
                //获取图片bitmap
                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
            }

            @Override
            public void recordSuccess(String s, Bitmap bitmap) {
                VideoEvent videoEvent = new VideoEvent();
                videoEvent.path = s;
                videoEvent.imagePath = saveBitmap(bitmap, IMAGE_PATH,CameraActivity.this);
                EventBus.getDefault().post(videoEvent);
                //获取视频路径
                Log.i("CJT", "url = " + s);
                finish();
            }
        });

        //左边按钮点击事件
        jCameraView.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                CameraActivity.this.finish();
            }
        });
        //右边按钮点击事件
        jCameraView.setRightClickListener(new ClickListener() {
            @Override
            public void onClick() {
                showLoading();
            }
        });




    }

    @Override
    protected void onResume() {
        super.onResume();
        jCameraView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        jCameraView.onPause();
    }

    @Override
    public void initData() {
    }

    @Override
    public void finish() {
        super.finish();
        AnimUtil.overrideSlideDownCloseAnimation(this);
    }

    public static String saveBitmap(Bitmap bitmap, String path, Context context) {
        // 首先保存图片
        File appDir = new File(path);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = "image"+System.currentTimeMillis() + ".png";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (context!=null){
            // 把文件插入到系统图库
            try {
                MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 通知图库更新
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + "/sdcard/namecard/")));
        }
        return file.getAbsolutePath();
    }


}
